﻿using Motions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tool;

namespace AutoMachine
{
    public partial class Frm_IO : Form
    {
        Tool.AutoResize FormResize = new Tool.AutoResize();//窗体缩放实例化
        public Frm_IO()
        {
            InitializeComponent();
            FormResize.SetAutoResize(this);//窗体缩放实例化
        }

        #region 窗体实例化
        /// <summary>
        /// 窗体对象实例化
        /// </summary>
        private static Frm_IO _instance;
        public static Frm_IO Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Frm_IO();
                return _instance;
            }
        }
        #endregion

        private void Form2_Load(object sender, EventArgs e)
        {
            SystemCfg.ReadCardXmlToDGV(this.dgvIN1, "IN");
            SystemCfg.ReadCardXmlToDGV(this.dgvOUT1, "OUT");
            //SystemCfg.ReadAxisNameXmlToDGV(this.dgvAxisName, "Axis");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //if (MotionMgr.GetInstance().Card.CardIniOK)
            //{
                #region 输入状态更新
                //状态更新
                for (int i = 0; i < dgvIN1.Rows.Count; i++)
                {
                    //int index = Convert.ToInt16(this.dgvIN1.Rows[i].Cells[2].Value.ToString());   //输入IO点位号

                    string Name = this.dgvIN1.Rows[i].Cells[3].Value.ToString().Trim(); //点位名称

                    if (MotionMgr.GetInstance().ReadIn(Name) == true)//输入ON 改成输入输出
                    {
                        Tool.CtrDisplay.ShowText(dgvIN1, i, 4, "True", Color.Green, Color.Empty);
                    }
                    else
                    {
                        Tool.CtrDisplay.ShowText(dgvIN1, i, 4, "False", Color.White, Color.Empty);
                    }
                }
                #region 输出状态更新
                //for (int i = 0; i < dgvOUT1.Rows.Count; i++)
                //{
                //    int index = Convert.ToInt16(this.dgvOUT1.Rows[i].Cells[2].Value.ToString());   //输出IO点位号

                //    string Name = this.dgvOUT1.Rows[i].Cells[3].Value.ToString().Trim(); //点位名称

                //    if (MotionMgr.GetInstance().ReadOut(Name) == true)//输出ON 改成输入输出
                //    {
                //        Tool.CtrDisplay.ShowText(dgvOUT1, i, 4, "True", Color.Green, Color.Empty);
                //    }
                //    else
                //    {
                //        Tool.CtrDisplay.ShowText(dgvOUT1, i, 4, "False", Color.White, Color.Empty);
                //    }
                //}

                #endregion

                #endregion
            //}

        }


        private void timer2_Tick(object sender, EventArgs e)
        {
          
        }

        private void dgvOUT1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 5 && e.RowIndex != -1)//第3列 任意行
            {
                int Row = e.RowIndex;//得到第几行
                //int Cardno = Convert.ToInt16(this.dgvOUT1.Rows[Row].Cells[0].Value.ToString());
                //int index = Convert.ToInt16(this.dgvOUT1.Rows[Row].Cells[2].Value.ToString()); //输出IO点位号
                string Name = this.dgvOUT1.Rows[Row].Cells[3].Value.ToString().Trim(); //点位名称

                MotionMgr.GetInstance().WriteOut(Name, true);
            }
            if (e.ColumnIndex == 6 && e.RowIndex != -1)//4列 任意行
            {
                int Row = e.RowIndex;//得到第几行     
                //int index = Convert.ToInt16(this.dgvOUT1.Rows[Row].Cells[2].Value.ToString()); //输出IO点位号
                string Name = this.dgvOUT1.Rows[Row].Cells[3].Value.ToString().Trim(); //点位名称
                MotionMgr.GetInstance().WriteOut(Name, false);
            }
        }

        private void Frm_AxisIO_FormClosing(object sender, FormClosingEventArgs e)
        {
            Frm_IO._instance = null;
        }
    }
}
